package com.situ.water_user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.util.ResponseData;
import com.situ.water_BE.pojo.BeOrderlite;
import com.situ.water_BE.service.BeOrderliteService;
import com.situ.water_read.service.RdReadService;
import com.situ.water_read.service.RdVolumeService;
import com.situ.water_system.pojo.SyArea;
import com.situ.water_system.pojo.SyCosttype;
import com.situ.water_system.pojo.SyEmp;
import com.situ.water_system.pojo.SyMetertype;
import com.situ.water_system.service.SyCosttypeService;
import com.situ.water_system.service.SyEmpService;
import com.situ.water_system.service.SyMetertypeService;
import com.situ.water_user.pojo.UsMeter;
import com.situ.water_user.pojo.UsUser;
import com.situ.water_user.service.UsFormulaService;
import com.situ.water_user.service.UsMeterService;
import com.situ.water_user.service.UsUserService;
import com.situ.water_user.util.CreateMeterNo;
import com.situ.water_user.util.LiteAndEmp;
import com.situ.water_user.util.LiteTJ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Angel
 * @since 2020-12-01
 */
@RestController
@RequestMapping("/us-user")
public class UsUserController {
    @Autowired
    private UsUserService service;

    @Autowired   //用户
            UsUserService usUserService;
    @Autowired   //水表
            UsMeterService usMeterService;
    @Autowired   //提比提量
            UsFormulaService usFormulaService;
    @Autowired   //水表类型
            SyMetertypeService syMetertypeService;
    @Autowired  //费用类型
            SyCosttypeService syCosttypeService;
    @Autowired    //员工
            SyEmpService syEmpService;
    @Autowired   //表册
            RdVolumeService rdVolumeService;
    @Autowired   //抄表数据
            RdReadService rdReadService;
    @Autowired  //快捷工单
            BeOrderliteService beOrderliteService;



    /**
     * @author lwt
     * 抄表管理查询所有用户
     */
    @GetMapping("/queryAlllwt")
    public ResponseData queryAlllwt(UsUser user,@RequestParam(defaultValue ="1",required = false) int current ,
                                    @RequestParam(defaultValue = "5",required = false) int size){
//        QueryWrapper<UsUser> wrapper = new QueryWrapper<>();
//        if (user.getUsername()!=null&&!"".equals(user.getUsername())) {
//            wrapper.like("username", user.getUsername());
//        }
        Page<UsUser> page=new Page<>(current,size);
        // List<UsUser> list=service.queryalllwt();
        // IPage<UsUser> page1 = service.page(page, wrapper);
        IPage<UsUser> page1=service.queryalllwt(page,user);
        //    list.forEach(x-> System.out.println(x.getUsername()));
        //   map.put("list",list);
        // System.out.println(user.getVolumeID());
        return  ResponseData.ok().putDataValue("page",page1);
    }



    //查询全部
    @GetMapping("/user/show")
    public PageInfo show(UsUser user,
                         @RequestParam(defaultValue = "1",required = false) int pageNum ,
                         @RequestParam(defaultValue = "5",required = false) int pageSize){
        QueryWrapper<UsUser> wrapper=new QueryWrapper<UsUser>();
        if(user!=null){
            System.out.println("show");
            System.out.println(user.getUsername());
            if(user.getUserno()!=null&&!user.getUserno().equals("")){
                wrapper.like("userno", user.getUserno());
            }
            if(user.getAbc()!=null&&!user.getAbc().equals("")){
                wrapper.like("abc", user.getAbc());
            }
            if(user.getUsername()!=null&&!user.getUsername().equals("")){
                wrapper.like("username", user.getUsername());
            }
            if(user.getPhone()!=null&&!user.getPhone().equals("")){
                wrapper.like("phone", user.getPhone());
            }
            if(user.getAddress()!=null&&!user.getAddress().equals("")){
                wrapper.like("address", user.getAddress());
            }

        }

        PageInfo pageInfo = usUserService.pageUser(wrapper, pageNum, pageSize);


        return pageInfo;
    }

    //选择框查询
    @GetMapping("/user/addSC")
    public Map addSC(){
        //所属辖区
        List<SyArea> salist = usUserService.querySyArea();
        //水表类型
        List<SyMetertype> materlist = syMetertypeService.queryAll();


        Map map=new HashMap();
        Date d=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String time=sdf.format(d);

        map.put("salist", salist);
        map.put("khTime", time);
        map.put("materlist", materlist);

        return map;
    }

    //生成
    @GetMapping("/user/userID")
    public Map userID(String xq){
        System.out.println("userId");
        Map map=new HashMap();
        int userID = usUserService.userID();
        //生成用户id
        String userId = CreateMeterNo.getUserNo(xq, userID+"");

        //生成水表id
        String materId="M"+userId+"-"+"01";
        map.put("userId", userId);
        map.put("materId",materId);
        return map;
    }

    //查询提比提量值
    @GetMapping("/user/tbtl")
    public List<SyCosttype> tbtl(){
        System.out.println("tbtl");
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("kind", 0);
        wrapper.eq("diskabled", 0);
        List<SyCosttype> costTypeList = syCosttypeService.queryTbTl(wrapper);
        return costTypeList;
    }



    //添加
    @PostMapping("/user/addUser")
    public void addUser(UsUser user, UsMeter mater){
        System.out.println("addUser");
        System.out.println(mater.getMeterNo());
        usUserService.addUser(user);
        mater.setUserNo(user.getUserno());
        usMeterService.addMeter(mater);
    }



    //根据id查询
    @GetMapping("/user/queryById")
    public Map queryById(UsUser user,
                         @RequestParam(defaultValue = "1",required = false) int pageNum ,
                         @RequestParam(defaultValue = "5",required = false) int pageSize){
        PageHelper.startPage(pageNum, pageSize);

        Map map=new HashMap();
        QueryWrapper<UsUser> wrapper=new QueryWrapper<UsUser>();
        if(user!=null){
            System.out.println("queryOne");
            System.out.println(user.getUsername());
            if(user.getUserno()!=null&&!user.getUserno().equals("")){
                wrapper.like("userno", user.getUserno());
            }
            if(user.getAbc()!=null&&!user.getAbc().equals("")){
                wrapper.like("abc", user.getAbc());
            }
            if(user.getUsername()!=null&&!user.getUsername().equals("")){
                wrapper.like("username", user.getUsername());
            }
            if(user.getPhone()!=null&&!user.getPhone().equals("")){
                wrapper.like("phone", user.getPhone());
            }
            if(user.getAddress()!=null&&!user.getAddress().equals("")){
                wrapper.like("address", user.getAddress());
            }
        }


        //用户基本信息
        UsUser usUser = usUserService.queryOne(wrapper);
        //用户水表信息
        UsMeter usMeter = usMeterService.queryByUserNo(usUser.getUserno());
        String meterId = CreateMeterNo.getMeterNo(usUser.getUserno(),usMeter.getMeterNo());
        usMeter.setMeterNo(meterId);
        System.out.println(usMeter.getMeterNo());
        //用户表册信息
        Map vmap = rdVolumeService.queryUserNo(usUser.getUserno());
        //抄表信息
        List<Map> rmaplist = rdReadService.queryUserNo(usUser.getUserno());
        PageInfo pageInfo = new PageInfo<Map>(rmaplist,5);
        //水表类型
        List<SyMetertype> smlist = syMetertypeService.queryAll();
        map.put("usUser", usUser);
        map.put("usMeter", usMeter);
        map.put("vmap", vmap);
        map.put("pageInfo", pageInfo);
        map.put("smlist", smlist);
        return map;
    }



    //根据id查询（传对象）
    @GetMapping("/user/queryOne")
    public Map queryOne(UsUser user,
                        @RequestParam(defaultValue = "1",required = false) int pageNum ,
                        @RequestParam(defaultValue = "5",required = false) int pageSize){
        PageHelper.startPage(pageNum, pageSize);

        Map map=new HashMap();
        QueryWrapper<UsUser> wrapper=new QueryWrapper<UsUser>();
        if(user!=null){
            System.out.println("queryOne");
            System.out.println(user.getUsername());
            if(user.getUserno()!=null&&!user.getUserno().equals("")){
                wrapper.like("userno", user.getUserno());
            }
            if(user.getAbc()!=null&&!user.getAbc().equals("")){
                wrapper.like("abc", user.getAbc());
            }
            if(user.getUsername()!=null&&!user.getUsername().equals("")){
                wrapper.like("username", user.getUsername());
            }
            if(user.getPhone()!=null&&!user.getPhone().equals("")){
                wrapper.like("phone", user.getPhone());
            }
            if(user.getAddress()!=null&&!user.getAddress().equals("")){
                wrapper.like("address", user.getAddress());
            }
        }


        //用户基本信息
        UsUser usUser = usUserService.queryOne(wrapper);
        //用户水表信息
        UsMeter usMeter = usMeterService.queryByUserNo(usUser.getUserno());
        String meterId = CreateMeterNo.getMeterNo(usUser.getUserno(),usMeter.getMeterNo());
        usMeter.setMeterNo(meterId);
        System.out.println(usMeter.getMeterNo());
        //用户表册信息
        Map vmap = rdVolumeService.queryUserNo(usUser.getUserno());
        //抄表信息
        List<Map> rmaplist = rdReadService.queryUserNo(usUser.getUserno());
        PageInfo pageInfo = new PageInfo<Map>(rmaplist,5);
        //水表类型
        List<SyMetertype> smlist = syMetertypeService.queryAll();

        map.put("usUser", usUser);
        map.put("usMeter", usMeter);
        map.put("vmap", vmap);
        map.put("pageInfo", pageInfo);
        map.put("smlist", smlist);

        return map;
    }

    //快捷过户
    @PostMapping("/user/ghUser")
    public void ghUser(UsUser user){
        System.out.println("ghUser");

        usUserService.ghUser(user);
        //添加操作记录
        BeOrderlite lite=new BeOrderlite();
        lite.setOrderType(2);
        //操作时间
        Date d=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String datetime = sdf.format(d);
        lite.setDatee(Timestamp.valueOf(datetime));
        lite.setEmpID(4);
        lite.setUserNo(user.getUserno());
        lite.setUserName(user.getUsername());
        lite.setDescription("过户给："+user.getUsername());
        beOrderliteService.addLite(lite);

    }

    //快捷代扣
    @PostMapping("/user/dkUser")
    public void dkUser(UsUser user){
        System.out.println("dkUser");
        usUserService.dkUser(user);
        //添加操作记录
        BeOrderlite lite=new BeOrderlite();
        lite.setOrderType(3);
        //操作时间
        Date d=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String datetime = sdf.format(d);
        //       System.out.println(Timestamp.valueOf(datetime));
        lite.setDatee(Timestamp.valueOf(datetime));
        lite.setEmpID(4);
        lite.setUserNo(user.getUserno());
        lite.setUserName(user.getUsername());
        if(user.getPayType().equals("银行代扣")){
            lite.setDescription("更改为："+user.getPayType()+",开户行："+user.getBankName()+",账号："+user.getBankNum());
        }else{
            lite.setDescription("更改为："+user.getPayType());
        }
        System.out.println(lite);
        beOrderliteService.addLite(lite);
    }

    //快捷换表
    @PostMapping("/user/hbMeter")
    public void hbMeter(UsMeter meter){
        System.out.println("hbMeter");
        usMeterService.hbMeter(meter);
        //添加操作记录
        BeOrderlite lite=new BeOrderlite();
        lite.setOrderType(2);
        //操作时间
        Date d=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String datetime = sdf.format(d);
        lite.setDatee(Timestamp.valueOf(datetime));
        lite.setEmpID(4);
        lite.setUserNo(meter.getUserNo());
        QueryWrapper<UsUser> wrapper=new QueryWrapper<UsUser>();
        wrapper.eq("userNo", meter.getUserNo());
        UsUser usUser = usUserService.queryOne(wrapper);
        lite.setUserName(usUser.getUsername());
        //水表类型
        SyMetertype syMeterType = syMetertypeService.queryById(meter.getMeterTypeID());
        lite.setDescription("新表类型："+syMeterType.getMeterTypeName()+",表码身："+meter.getMeterName());
        beOrderliteService.addLite(lite);
    }



    //快捷重签
    @PostMapping("/user/cqUser")
    public void cqUser(UsUser user,String oldFormula){
        System.out.println("cqUser");
        usUserService.cqUser(user);
        //添加操作记录
        BeOrderlite lite=new BeOrderlite();
        lite.setOrderType(5);
        //操作时间
        Date d=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String datetime = sdf.format(d);
        lite.setDatee(Timestamp.valueOf(datetime));
        lite.setEmpID(4);
        lite.setUserNo(user.getUserno());
        lite.setUserName(user.getUsername());
        lite.setDescription("旧提比提量："+oldFormula+"   新提比提量："+user.getFormula());
        beOrderliteService.addLite(lite);
    }

    //快捷销户
    @PostMapping("/user/xhUser")
    public void xhUser(UsUser user){
        System.out.println("xhUser");
        user.setDisabled(true);
        usUserService.xhUser(user);
        //添加操作记录
        BeOrderlite lite=new BeOrderlite();
        lite.setOrderType(6);
        //操作时间
        Date d=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String datetime = sdf.format(d);
        lite.setDatee(Timestamp.valueOf(datetime));

        lite.setEmpID(4);
        lite.setUserNo(user.getUserno());
        lite.setUserName(user.getUsername());
        lite.setDescription("销户说明："+lite.getDescription());
        beOrderliteService.addLite(lite);
    }

    //档案号管理
    @PostMapping("/user/dahUser")
    public void dahUser(UsUser user){
        System.out.println("dahUser");
        usUserService.dahUser(user);
    }


    //快捷操作记录
    @GetMapping("/user/queryLitePage")
    public Map queryLitePage(LiteTJ tj,
                             @RequestParam(defaultValue = "1",required = false) int pageNum ,
                             @RequestParam(defaultValue = "5",required = false) int pageSize){
        System.out.println("queryLitePage"+tj.getMaxDate()+"---"+tj.getMinDate());
        Map map=new HashMap();
        PageHelper.startPage(pageNum, pageSize);
        //快捷操作记录
        List<LiteAndEmp> liteList = beOrderliteService.queryPageLite(tj);
        PageInfo pageInfo = new PageInfo<LiteAndEmp>(liteList,5);
        //员工
        List<SyEmp> empList = syEmpService.selectList();
        map.put("pageInfo", pageInfo);
        map.put("empList", empList);
        return map;
    }
}

